class UserMailer < ActionMailer::Base
  default :content_type => 'text/html'
  #default :sent_on =>  Time.now
  default :from => '"NoiseTube" <support@noisetube.net>'

  def signup_notification(user)
    setup_email(user)
    @subject    += 'Please activate your new account'
       @url  = "activate/#{user.activation_code}"
  end


  def activation(user)
    setup_email(user)
    @subject    += 'Your account has been activated!'
    @url  = "/"
  end


  def deliver_new_track(track)

    @user=track.user

    setup_email @user

    mail(:to => @user.email,
         :subject => "Your NoiseTube track has been processed")
  end

  def forgot_password(user)
    @user = user
    setup_email(user)
    logger.info("About to send email to #{user.login}....")
    #Delayed::Worker.logger.add(Logger::INFO, "DELAYED JOB >> Finished setting up email")
    subject = "Reset your NoiseTube account password"
    @url = Rails.env == "production" ? "http://www.noisetube.net/reset_password/#{user.password_reset_code}" :
                                       "#{root_url}/reset_password/#{user.password_reset_code}"
    mail({:to => user.email, :subject => subject})
    logger.info("Rendered template for forgot_password of #{user.login} to #{user.email}....")
  end

  def deliver_new_aggmap(map)

    @user=map.user

    setup_email @user

    mail(:to => @user.email,
         :subject => "Your NoiseTube map '#{map.try(:name)}' has been generated")
  end

  #can do without this..
  def reset_password(user)
    setup_email(user)
    recipients "#{user.email}"
    body({:user => user})
    subject 'Your password has been reset.'
  end

  protected

  def setup_email(user)
    @recipients  = "#{user.email}"
    @from        = '"NoiseTube" <support@noisetube.net>'
    @subject     = "NoiseTube - Notification for #{user.login} "
    @sent_on     = Time.now
    @user = user
  end

  def test_email
    mail(:from => '"NoiseTube" <support@noisetube.net>', :to => "itsakworld@gmail.com", :subject => "Test mail", :body => "Test mail body")
  end

end
